package com.primaryedu.ui.exercise;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.primaryedu.R;
import com.primaryedu.adapter.CommentAdapter;
import com.primaryedu.bean.CommentBean;
import com.primaryedu.data.Contants;
import com.primaryedu.ui.BaseActivity;
import com.primaryedu.utils.Utils;
import com.primaryedu.widget.MyListView;
import com.primaryedu.widget.PullToRefreshView;
import com.primaryedu.widget.PullToRefreshView.OnFooterRefreshListener;
import com.primaryedu.widget.PullToRefreshView.OnHeaderRefreshListener;

/**
 * 评论窗体
 *
 * @author jun.wang
 * @description
 * @data 2016年12月19日
 */
public class CommentActivity extends BaseActivity implements OnClickListener, OnHeaderRefreshListener, OnFooterRefreshListener {
    private MyListView listView;
    CommentAdapter adapter;
    private String newId = "";
    private String type = "";
    List<CommentBean> list;
    List<CommentBean> tempList = new ArrayList<>();

    private PullToRefreshView pullToRefreshView;
    private int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_comment);

        newId = getIntent().getStringExtra("newId");
        type = getIntent().getStringExtra("type");
        init();
    }

    private void init() {
        ((TextView) findViewById(R.id.title)).setText("评论");
        findViewById(R.id.back).setOnClickListener(this);
//		findViewById(R.id.layout).setOnClickListener(this);

        listView = (MyListView) this.findViewById(R.id.listView);
        list = new ArrayList<>();

        pullToRefreshView = (PullToRefreshView) this.findViewById(R.id.main_pull_refresh_view);
        pullToRefreshView.setOnHeaderRefreshListener(this);
        pullToRefreshView.setOnFooterRefreshListener(this);
        pullToRefreshView.setLastUpdated(new Date().toLocaleString());

    }

    @Override
    protected int BindLayout() {
        return R.layout.activity_comment;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        requestData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;

            case R.id.layout:
                Intent intent = new Intent(CommentActivity.this, CommentWriteActivity.class);
                intent.putExtra("newId", newId);
                intent.putExtra("type", type);   // 新闻
                startActivity(intent);
                break;

            default:
                break;
        }
    }

    private void requestData() {
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Contants.getComment,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        System.out.println("getComment==" + response);
                        closeDialog();
                        try {
                            JSONObject json = new JSONObject(response);
                            int result = json.getInt("result");
                            String reason = json.getString("reason");

                            if (result == 1) {
                                String data = json.getString("data");
                                JSONObject dataObj = new JSONObject(data);
                                String listStr = dataObj.getString("list");

                                list = new Gson().fromJson(listStr,
                                        new TypeToken<List<CommentBean>>() {
                                        }.getType());
                                System.out.println("getComment=00=" + list.toString());
                                tempList.addAll(list);

                                if (null != tempList && tempList.size() != 0) {
                                    adapter = new CommentAdapter(CommentActivity.this, Utils.getCommonList(tempList));
                                    listView.setAdapter(adapter);
                                }

                            } else {
                                showToast(reason);
                            }


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                //在这里设置需要post的参数
                Map<String, String> map = new HashMap<String, String>();
                map.put("objectid", newId);
                map.put("type", type);
                map.put("pageNo", page + "");
                map.put("pageSize", "10");
                System.out.println("getComment=00=" + map.toString());
                return map;
            }
        };
        requestQueue.add(stringRequest);
    }

    @Override
    public void onFooterRefresh(PullToRefreshView view) {
        pullToRefreshView.postDelayed(new Runnable() {

            @Override
            public void run() {
                pullToRefreshView.onFooterRefreshComplete();
                page++;
                requestData();
            }

        }, 1500);
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        pullToRefreshView.postDelayed(new Runnable() {
            @Override
            public void run() {
                requestData();

                pullToRefreshView.onHeaderRefreshComplete("更新于:"
                        + Calendar.getInstance().getTime().toLocaleString());
                pullToRefreshView.onHeaderRefreshComplete();

                Toast.makeText(CommentActivity.this, "数据刷新完成!", 0).show();
            }

        }, 1500);
    }

}
